package com.team.childapp.ui

import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.provider.Settings
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.team.childapp.R
import com.team.childapp.adapter.GuideAdapter
import com.team.childapp.base.activity.BaseActivity
import com.team.childapp.databinding.ActivityWelcomBinding
import com.team.childapp.ui.login.RegisterActivity
import com.team.childapp.util.DimensionUtil

/**
 * Create on: 2025/1/17
 * by Mr.Zhu
 * Description: TODO
 */
class WelcomeActivity : BaseActivity<ActivityWelcomBinding>(R.layout.activity_welcom) {
    private val imgs = intArrayOf(R.mipmap.splash_1, R.mipmap.splash_2, R.mipmap.splash_3)
    private var lastPointIndex = 0
    override fun initData() {
        initDataImage()
        initEvent()
    }


    private fun initDataImage() {
        val imageViews = ArrayList<ImageView>()
        var imageView: ImageView? = null
        var pointView: View? = null
        var layoutParams: LinearLayout.LayoutParams? = null
        for (i in imgs.indices) {
            //添加图片
            imageView = ImageView(this)
            imageView.layoutParams =
                ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
            imageView.setImageResource(imgs.get(i))
            imageViews.add(imageView)

            //添加指针
            pointView = View(this)
            pointView.setBackgroundResource(R.drawable.point_seleoter)
            //指针的宽度和高度
            layoutParams =
                LinearLayout.LayoutParams(DimensionUtil.dpToPx(5), DimensionUtil.dpToPx(5))

            if (i == 0) {
                pointView.isEnabled = true
            } else {
                //指针距离左边的间距
                layoutParams!!.leftMargin = DimensionUtil.dpToPx(5)
                pointView.isEnabled = false
            }

            mBinding.guideActivityLlPoints.addView(pointView, layoutParams)
        }
        val guideAdapter: GuideAdapter = GuideAdapter(imageViews)
        mBinding.viewPage.setAdapter(guideAdapter)
    }


    private fun initEvent() {
        mBinding.viewPage.setOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(i: Int, v: Float, i1: Int) {
            }

            override fun onPageSelected(i: Int) {
                mBinding.guideActivityLlPoints.getChildAt(lastPointIndex).setEnabled(false)
                //将当前的点选中
                mBinding.guideActivityLlPoints.getChildAt(i).isEnabled = true

                if (i == 2) {
                    mBinding.guideActivityBtExperienceNow.visibility = View.VISIBLE
                } else {
                    mBinding.guideActivityBtExperienceNow.visibility = View.GONE
                }
                lastPointIndex = i
            }

            override fun onPageScrollStateChanged(i: Int) {
            }
        })

        mBinding.guideActivityBtExperienceNow.setOnClickListener(View.OnClickListener {
            if (!isNetworkAvailable(this)) {
                openInternetPanel(this)
            }else {
                start<RegisterActivity>()
                finish()
            }
        })
    }

    //检测是否有网络
    fun isNetworkAvailable(context: Context): Boolean {
        val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val network = connectivityManager.activeNetwork ?: return false
        val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false
        return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
    }

    //打开wifi设置
    fun openInternetPanel(context: Context) {
        val intent = Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY)
        context.startActivity(intent)
    }

    override fun onResume() {
        super.onResume()
        if(lastPointIndex==2) {
            if (!isNetworkAvailable(this)) {
                openInternetPanel(this)
            } else {
                start<RegisterActivity>()
                finish()
            }
        }
    }
    // 重写 onKeyDown 方法
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
//        setImageSelected(currentSelectedIndex, false)
        return when (keyCode) {

            KeyEvent.KEYCODE_DPAD_UP -> {
                selectPage(1)
                true // 表示事件已处理
            }


            KeyEvent.KEYCODE_DPAD_DOWN -> {
                selectPage(0)
                true // 表示事件已处理
            }

            else -> super.onKeyDown(keyCode, event) // 其他按键交给系统处理

        }


    }


    /***
     *  directionType 滚轮转动方向，1是向前，0是向后
     */
    fun selectPage(directionType:Int){
        val currentItem = mBinding.viewPage.currentItem
        var nextItem=currentItem+1;
        if(directionType==0){
            nextItem=currentItem-1
            if(currentItem<=0){
                nextItem=2
            }
        }else if(directionType==1){
            if(currentItem>=2){
                nextItem=0
            }
        }
        mBinding.viewPage.setCurrentItem(nextItem, true) // true 表示启用平滑滚动
        mBinding.guideActivityLlPoints.getChildAt(lastPointIndex).setEnabled(false)
        //将当前的点选中
        mBinding.guideActivityLlPoints.getChildAt(currentItem).isEnabled = true

        if (currentItem == 2) {
            mBinding.guideActivityBtExperienceNow.visibility = View.VISIBLE
        } else {
            mBinding.guideActivityBtExperienceNow.visibility = View.GONE
        }
        lastPointIndex = currentItem
    }
}